package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @Author mr.wu
 * @Data #{DATE} #{TIME}
 */
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping()
    @ApiOperation("新增分类")
    public Result<CategoryDTO> save(@RequestBody CategoryDTO categoryDTO){
        if(categoryService.save(categoryDTO)!=0) {
            return Result.success();
        }
         return Result.error("新增失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页查询分类列表")
    public Result<PageResult> getPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询：{}", categoryPageQueryDTO);
        PageResult result = categoryService.getPage(categoryPageQueryDTO);
        return Result.success(result);
    }

    @DeleteMapping()
    @ApiOperation("删除分类")
    public Result deleteById(Long id){
        log.info("删除分类：{}", id);
        if(categoryService.deleteById(id)!=0){
           return Result.success();
        }
        return Result.error("删除失败");
    }

    @PutMapping()
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        if(categoryService.update(categoryDTO)!=0){
            return Result.success();
        }
        return Result.error("修改失败");
    }

    @GetMapping("/list")
    @ApiOperation("查询分类列表")
    public Result<List<Category>> getList(Integer type){
        List<Category> list = categoryService.getList(type);
        return Result.success(list);
    }

}
